<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/6/15
 * Time: 11:25
 */

class Banner extends M_Controller
{
    public function main(){
        list($data, $total, $param,$nogroud,$ground)	= $this->_limit_page(); // 数据库中分页查询
//        $list = $this->db->where("1=1")->get("banner_manage")->result_array();
        $this->template->assign(
            array(
                'list'=>$data,
                'total' => $total,
                "ground"=>$ground,
                'param' => $param,
                "course_type"=>array("1V4","1V1","免费试听","公开课","专题课"),
                'pages'	=> $this->get_pagination(dr_url('admin/banner/main', array(
                    'total' => $total,
                    'search' => IS_POST ? 1 : $this->input->get('search')
                )), $total)
            )
        );
        $this->template->display("./banner/index.html");

    }
    //banner 添加
    public function add(){
        if(IS_POST){
//            print_r($_POST);exit;
            $data = $this->input->get_post("data");
            $client = $this->input->get_post("client");
            $part  = $this->input->get_post("part");
            $img = $this->input->get_post("upload_img");
            $skip = $this->input->get_post('skip');
            $start_time = $this->input->get_post('start_time');
            $end_time = $this->input->get_post('end_time');
            $is_shelves = $this->input->get_post('is_shelves');
            if(!empty($img)){
                $data['cover'] = $img;
            }
            $data['skip'] = $skip;
            $data['client'] = $client;
            $data['part'] = $part;
            $data['addtime'] = time();
            //$data['start_time'] = date('Y-m-d',$data['start_time']);
            $data['start_time'] = $start_time;
            $data['end_time'] = $end_time;
            $data['is_shelves'] = $is_shelves;
            $result = $this->db->insert("banner_manage",$data);
            if($result){
                $this->admin_msg('添加banner成功', dr_url(APP_DIR.'/'.$this->router->class.'/main', $this->post_param), 1);
            }

        }
        $this->template->assign(array(
            "name"=>"基本信息",
            "banners"=>array("首页","精品小班","量身定制","户学名师","公开课"),
        ));
        $this->template->display("banner/add.html");
    }
    //编辑
    public function edit(){
        $id = $this->input->get_post("id");
        if(IS_POST){
            $data = $this->input->get_post("data");
            $client = $this->input->get_post("client");
            $part  = $this->input->get_post("part");
            $img = $this->input->get_post("upload_img");
            $skip = $this->input->get_post('skip');
            $is_shelves = $this->input->get_post('is_shelves');
            if(!empty($img)){
                $data['cover'] = $img;
            }
            $data['skip'] = $skip;
            $data['client'] = $client;
            $data['part'] = $part;
            $data['addtime'] = time();
            $data['start_time'] = $this->input->get_post('start_time');
            $data['end_time'] = $this->input->get_post('end_time');
            $data['is_shelves'] = $is_shelves;
            $result = $this->db->where("id",$id)->update("banner_manage",$data);
            if($result){
                $this->admin_msg('修改banner成功', dr_url(APP_DIR.'/'.$this->router->class.'/main', $this->post_param), 1);
            }
        }
        $data = $this->db->where("id",$id)->get("banner_manage")->row_array();
        if(empty($data)){
            $this->msg("该banner不存在或者已经删除");
        }
        $this->template->assign(array(
            "data"=>$data,
            "banners"=>array("首页","精品小班","量身定制","户学名师","公开课"),
        ));
        $this->template->display('banner/banner_edit.html');
    }
    //上架 下架
    public function moveground(){
        //$getDate= date("Y-m-d");

        $ground = $this->input->get_post('ground');
        $course_id = $this->input->get_post("course_id");

        $data = $this->db->where("id",$course_id)->get("banner_manage")->row_array();
        if(empty($data)){
            $this->msg('该banner已经删除或者不存在');
        }
        $para = array();
        $data = array();
        if($ground){
            $para = array("is_ground"=>0);
            $data['message'] = "下架成功";
        }else{
            $para = array("is_ground"=>1);
            $data['message'] = "上架成功";
        }

        $result = $this->db->where("id",$course_id)->update("banner_manage",$para);
        if($result){
            $data['status'] = 1;
            $data['msg'] = "修改成功";
        }else{
            $data['status'] = 0;
            $data['msg'] = "修改失败";
        }
        echo json_encode($data);exit;
    }


    //删除
    public function del(){
        $id = $this->input->get_post("cid");
        $data = $this->db->where("id", $id)->get("banner_manage")->row_array();
        if(empty($data)){
            $this->msg("该banner不存在或者已经删除");
        }
        $result = $this->db->where("id",$id)->update("banner_manage",array("is_del"=>0));
        if($result){
            $this->admin_msg('删除banner成功', dr_url(APP_DIR.'/'.$this->router->class.'/main', $this->post_param), 1,1);
        }
    }

    private function _where(&$select) {

        $uid = (int)$this->input->get('id');
        $uid = $uid ? $uid : $this->uid;
        $search = $this->input->get('search');

        $cache_file = md5($this->duri->uri(1).$uid.$this->uid.SITE_ID.$this->input->ip_address().$this->input->user_agent()); // 缓存文件名称

        // 存在POST提交时，重新生成缓存文件
        if (IS_POST) {
            $data = $this->input->post('data');
            $this->cache->file->save($cache_file, $data, 3600);
            $search = 1;
            unset($_GET['page']);
        }
//        $select->where('id', $uid);
        $select->where("is_del",1);
        if(isset($ground)){
            $select->where(array('is_ground'=>$ground,"is_del"=>1));
        }

        // 存在search参数时，读取缓存文件
        if ($search == 1) {
            $data = $this->cache->file->get($cache_file);
            if (isset($data['start']) && $data['start'] && $data['start'] != $data['end']) {
                $data['end'] = $data['end'] ? $data['end'] : SYS_TIME;
                $select->where('logintime BETWEEN '.$data['start'].' AND '. $data['end']);
            }
        }
//        $data['id'] = $uid;
        return $data;
    }

    private function  _limit_page(){
        $page = max((int)$this->input->get_post('page'),1);
        $_total = (int)$this->input->get_post('total');
        if(!$_total){
            $arr = $this->db->select("count(*) as count")->where("is_del",1)->get("banner_manage")->row_array();
            $_total = $arr['count'];
            if(!$_total){
//                return array(array(), 0 ,$_param);
                return array();
            }
        }
        //未上架的课程
        $select = $this->db;
        $_param = $this->_where($select);
        $result = $this->db
            ->limit(SITE_ADMIN_PAGESIZE, SITE_ADMIN_PAGESIZE * ($page - 1))
            ->order_by('id desc')
            ->get('banner_manage')
            ->result_array();
//        return array($result,$_total,$_param,$no_ground['count'],$ground['count']);
        return array($result,$_total,$_param);
    }



    //上传图片
    public function uploadImg(){

        $config['upload_path']      = './uploadfile/banner/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = '1024*20';
        $config['max_width']        = '20000';
        $config['max_height']       = '20000';
        $config['file_name']  = time(); //文件名不使用原始名

        $this->load->library('upload', $config);
//        var_dump($this->upload->do_upload('img'));exit;
        if (!$this->upload->do_upload('img'))
        {
            $error = array('error' => $this->upload->display_errors());
            $result['msg'] = $error  ;
            $result['status'] = 0;

//            print_r($error);exit;
//            $this->load->view('upload_form', $error);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $result = array();
            $result['img_url'] =   $config['upload_path'].$data['upload_data']['orig_name'];
            $result['status'] = 1;
//            echo json_encode($result);exit;
//            $this->load->view('upload_success', $data);
        }
        echo json_encode($result);exit;

    }

}